package com.realgodo.site.common;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;

import com.realgodo.site.entity.security.User;


public class MailServiceImpl implements MailService {

	private MailEngine mailEngine;

	private String url;
	
	private String loginUrl;
	
	private String domain;
	private String hot_tel;
	private String admin_Email;
	
//	private String template;
	
	private Map<String,String> templateMap;
	/**用户注册通知模板*/
	private final String REGIST_TEMPLATE="rejist_template";
	/**用户激活通知模板*/
	private final String ACTIVE_TEMPLATE="active_template";
	/**用户认证失败通知模板*/
	private final String REJECT_TEMPLATE="reject_template";

	@Autowired
	public void setMailEngine(MailEngine mailEngine) {
		this.mailEngine = mailEngine;
	}
	@Override
	public void sendRejistMail(User user) {
		SimpleMailMessage msg = new SimpleMailMessage();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", user);
		model.put("domain", domain);
		model.put("hot_tel", hot_tel);
		model.put("admin_Email", admin_Email);
		msg.setSubject("美美网 新用户注册信");
		msg.setTo(user.getEmail());
		try {
			mailEngine.sendMessage(msg, this.templateMap.get(REGIST_TEMPLATE), model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void sendAuthMail(User user) {
		SimpleMailMessage msg = new SimpleMailMessage();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", user);
		model.put("activeUrl", this.url + "?id="+ user.getId() + "&activeCode=" + user.getActiveCode());
		model.put("domain", domain);
		model.put("hot_tel", hot_tel);
		model.put("admin_Email", admin_Email);
		msg.setSubject("美美网 新用户 激活信");
		msg.setTo(user.getEmail());
		try {
			mailEngine.sendMessage(msg, this.templateMap.get(ACTIVE_TEMPLATE), model);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void sendRejectMail(User user) {
		SimpleMailMessage msg = new SimpleMailMessage();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", user);
		model.put("domain", domain);
		model.put("hot_tel", hot_tel);
		model.put("admin_Email", admin_Email);
		msg.setSubject("美美网 新用户 认证失败");
		msg.setTo(user.getEmail());
		try {
			mailEngine.sendMessage(msg, this.templateMap.get(REJECT_TEMPLATE), model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public String getLoginUrl() {
		return loginUrl;
	}


	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}


	public String getDomain() {
		return domain;
	}


	public void setDomain(String domain) {
		this.domain = domain;
	}


	public String getHot_tel() {
		return hot_tel;
	}


	public void setHot_tel(String hot_tel) {
		this.hot_tel = hot_tel;
	}


	public String getAdmin_Email() {
		return admin_Email;
	}


	public void setAdmin_Email(String admin_Email) {
		this.admin_Email = admin_Email;
	}


	public Map<String, String> getTemplateMap() {
		return templateMap;
	}


	public void setTemplateMap(Map<String, String> templateMap) {
		this.templateMap = templateMap;
	}



	
	

}
